<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Demo_basic extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
	}

	public function index()
	{
		// 扫码页面
		if ($this->input->is_ajax_request()) {
			if ($status = $this->input->input_stream('status')) {
				$return = json_encode(array('error' => 1, 'msg' => 'error: ' . $status));
			} else {
				switch ($this->input->input_stream('method')) {
					case 'shake_hands':
						// 服务器发送的握手回执
						if ($this->config->item('shake_hands_code') == decrypt($this->input->input_stream('code'))) {
							$return = array('error' => 0, 'msg' => '握手验证成功');
						} else {
							$return = array('error' => 1, 'msg' => '握手验证失败');
						}
						break;
					case 'register':
						// 服务器发出注册成功的通知
						$return = array('error' => 0, 'msg' => '注册成功');
						break;
					case 'scan':
						// 当二维码被扫描时，服务器发出此通知
						$return = array('error' => 0, 'msg' => '二维码已被扫描');
						break;
					case 'login':
						// 服务器发起的扫码登录请求
						if ($user = $this->input->input_stream('nickname')) {
							$return = array('error' => 0, 'msg' => $user);
						} else {
							$return = array('error' => 1, 'msg' => '扫码登录失败');
						}
						break;
					default:
						$return = array('error' => 1, 'msg' => '无效的 AJAX 请求');
				}
			}
			echo json_encode($return);
		} else {
			$client_id = uniqid();
			$data = array(
				'client_id' => $client_id, // 唯一的 ID，用来区分是通过哪个页面扫描的
				'shake_hands' => json_encode(array('method' => 'shake_hands', 'code' => encrypt($this->config->item('shake_hands_code')))), // 用于建立握手时的验证
				'register' => json_encode(array('method' => 'register', 'client_id' => encrypt($client_id))) // 生成页面注册申请（通过 js 向服务器发送）
			);
			$this->load->view('demo/basic/index', $data);
		}
	}

	public function login($client_id)
	{
		// 登录操作页面（通过扫码页面生成的）
		if ($this->input->is_ajax_request()) {
			if ($status = $this->input->input_stream('status')) {
				$return = json_encode(array('error' => 1, 'msg' => 'error: ' . $status));
			} else {
				switch ($this->input->input_stream('method')) {
					case 'shake_hands':
						// 服务器发送的握手回执
						if ($this->config->item('shake_hands_code') == decrypt($this->input->input_stream('code'))) {
							$return = array('error' => 0, 'msg' => '握手验证成功');
						} else {
							$return = array('error' => 1, 'msg' => '握手验证失败');
						}
						break;
					case 'scan':
						// 服务器对页面开启已经处理完毕
						$return = array('error' => 0, 'msg' => '扫码开启成功');
						break;
					case 'login':
						// 确认登录
						$return = array('error' => 0, 'msg' => json_encode(array('method' => 'login', 'client_id' => encrypt($client_id), 'nickname' => $this->input->input_stream('nickname'))));
						break;
					default:
						$return = array('error' => 1, 'msg' => '无效的 AJAX 请求');
				}
			}
			echo json_encode($return);
		} else {
			$data = array(
				'client_id' => $client_id, // 唯一的 ID，用来区分是通过哪个页面扫描的
				'shake_hands' => json_encode(array('method' => 'shake_hands', 'code' => encrypt($this->config->item('shake_hands_code')))), // 用于建立握手时的验证
				'scan' => json_encode(array('method' => 'scan', 'client_id' => encrypt($client_id))) // 向服务器发送页面已打开的回执（通过 js 向服务器发送）
			);
			$this->load->view('demo/basic/login', $data);
		}
	}

}